<?php
/**
 * 支付宝 控制器类
 */
namespace Home\Controller;
use Think\Controller;
class AlipayController extends Controller{
	public $alipay_config;
    
	//WEB充值异步处理
	public function notify_url(){
		// 支付配置
		$pay_config = D('Config')->get('', 'pay');
		//导入支付宝包
		import("WEBAlipay", 'Public/Class/Pay/Alipay');
		$alipay = new \WEBAlipay();
		$alipay->seller_email  = $pay_config['alipay_seller_email'];
		$alipay->partner       = $pay_config['alipay_partner'];
		$alipay->key           = $pay_config['alipay_key'];
		//获取支付宝配置
		$alipay_config = $alipay->config();
		$this->logs('[异步]========================订单处理========================');
		$this->logs('[异步]支付宝==>配置信息：' . print_r($alipay_config, true));

		$this->logs('[异步]支付宝==>GET数据：' . print_r($_GET, true));
		$this->logs('[异步]支付宝==>POST数据：' . print_r($_POST, true));
		//计算得出通知验证结果
		$alipayNotify  = new \AlipayNotify($alipay_config);
		$verify_result = $alipayNotify->verifyNotify();

		if($verify_result) {//验证成功
			$this->logs('[异步]支付宝==>验证成功：');
			//订单号
			$order_sn     = $_POST['out_trade_no'];
			//支付宝交易号
			$trade_no     = $_POST['trade_no'];
			//交易状态
			$trade_status = $_POST['trade_status'];
			//支付金额
			$total_fee    = $_POST['total_fee'];
			//买方支付账号
			$buyer_email  = $_POST['buyer_email'];
			//买方ID
			$buyer_id     = $_POST['buyer_id'];
			//卖方账号
			$seller_email = $_POST['seller_email'];

			//交易状态
			$trade_status = $_POST['trade_status'];
			if($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {
				$this->logs('[异步]验证订单是否可以支付');
				$where['order_sn'] = $order_sn;
				$order = D('Order')->where($where)->find();
				if(!$order){
					$this->logs('[异步]此订单号不存在！');
					exit("fail");
				}
				if($order['status'] != 'wait_pay'){
					$this->logs('[异步]此订单状态不能进行支付，订单状态：' . $order['status'] . '可支付状态：wait_pay');
					exit("fail");
				}
				$this->logs('[异步]提交支付成功订单......');
				//提交订单支付
				D('Order')->order_pay('alipay',
									  $order_sn,
									  $trade_no,
									  $total_fee,
									  $buyer_email,
									  $buyer_id,
									  $seller_email);
				
				$this->logs('[异步]支付成功');
				exit('success');
			}
		}
		//验证失败
		else {
			$this->logs('[异步]支付宝==>验证失败');
		    exit('fail');
		}
	}
	//WEB充值同步处理
	public function call_back(){
		// 支付配置
		$pay_config = D('Config')->get('', 'pay');
		//导入支付宝包
		import("WEBAlipay", 'Public/Class/Pay/Alipay');
		$alipay = new \WEBAlipay();
		$alipay->seller_email  = $pay_config['alipay_seller_email'];
		$alipay->partner       = $pay_config['alipay_partner'];
		$alipay->key           = $pay_config['alipay_key'];
		//获取支付宝配置
		$alipay_config = $alipay->config();
		$this->logs('[同步]支付宝==>配置信息：' . print_r($alipay_config, true));

		$this->logs('[同步]支付宝==>GET数据：' . print_r($_GET, true));
		//计算得出通知验证结果
		$alipayNotify  = new \AlipayNotify($alipay_config);
		$verify_result = $alipayNotify->verifyReturn();
		// 回调数据
		$callback_data = decode(urldecode($_GET['callback_data']));
		if($verify_result) {//验证成功
			$this->logs('[同步]支付宝==>验证成功');
			//商户订单号
			$order_sn     = $_GET['out_trade_no'];
			//支付宝交易号
			$trade_no     = $_GET['trade_no'];
			//支付金额
			$total_fee    = $_GET['total_fee'];
			//买方支付账号
			$buyer_email  = $_GET['buyer_email'];
			//买方ID
			$buyer_id     = $_GET['buyer_id'];
			//卖方账号
			$seller_email = $_GET['seller_email'];
			//交易状态
			$trade_status = $_GET['trade_status'];

			if($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {
				$this->logs('[同步]验证订单是否可以支付');
				$where['order_sn'] = $order_sn;
				$order = D('Order')->where($where)->find();
				if(!$order){
					$this->logs('[同步]此订单号不存在！');
					exit("fail");
				}
				if($order['status'] != 'wait_pay'){
					$this->logs('[同步]此订单状态不能进行支付，订单状态：' . $order['status'] . '可支付状态：wait_pay');
					exit("fail");
				}
				$this->logs('[同步]提交支付成功订单......');
				//提交订单支付
				D('Order')->order_pay('alipay',
									  $order_sn,
									  $trade_no,
									  $total_fee,
									  $buyer_email,
									  $buyer_id,
									  $seller_email);
				
				$this->logs('支付成功');

				redirect(URL('UserOrder', array('order_sn' => $order_sn)));
		    }
		}
		else {
			$this->logs('[同步]支付宝==>验证失败！');
		    exit("fail");
		}
	}
	//写入日志
	protected function logs($content){
		\Think\Log::write($content, \Think\Log::INFO, '',$this->log_path);
	}
}
?>